<?php if ( ! defined('SERVER_ROOT')) exit('No direct script access allowed');

/**
 * MVC Ultra - A ultralight weight MVC framework for PHP
 *
 * MVC ultra is very simple MVC framework that is designed to be easy to use and learn.
 * It does not have all of other frameworks but you add what you need when you need it
 * and understand better whats going on.
 *
 *@author Warren Thomas <wiley_warren@yahoo.com>
 *@copyright Copyright (c) 2012.
 *
 ** 20121027/Warren Thomas      - Created MVC Ultra
 *
 */

/**
 * ultra_db is the base controller class for MVC Ultra
 *
 * ultra_db is a wrapper class that provides static access to php's PDO class
 * It should to be extends to support mutilple Models for mvc architecture.
 *
 *@package core
 *
 */

class ultra_db {
    private static $link = null ;

    private static function getLink ( ) {
        if ( self :: $link ) {
            return self :: $link ;
        }
        $parse = ultra_config::getInstance();

        $driver = $parse [ "db_driver" ] ;
        $dsn = "${driver}:" ;
        $user = $parse [ "db_user" ] ;
        $password = $parse [ "db_password" ] ;
        $options = $parse [ "db_options" ] ;
        $attributes = $parse [ "db_attributes" ] ;

        foreach ( $parse [ "dsn" ] as $k => $v ) {
            $dsn .= "${k}=${v};" ;
        }

        self :: $link = new PDO ( $dsn, $user, $password, $options ) ;

        foreach ( $attributes as $k => $v ) {
            self :: $link -> setAttribute ( constant ( "PDO::{$k}" )
                , constant ( "PDO::{$v}" ) ) ;
        }

        return self :: $link ;
    }

    public static function __callStatic ( $name, $args ) {
        $callback = array ( self :: getLink ( ), $name ) ;
        return call_user_func_array ( $callback , $args ) ;
    }
}